package cn.tedu.user.management.dao.persist.repository.impl;

import cn.tedu.user.management.dao.persist.mapper.UserRoleMapper;
import cn.tedu.user.management.dao.persist.repository.IUserRoleRepository;
import cn.tedu.user.management.pojo.entity.UserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class UserRoleRepositoryImpl implements IUserRoleRepository {

    @Autowired
    UserRoleMapper userRoleMapper;
    @Override
    public int deleteByUserId(Long id) {
        log.debug("开始执行【根据user_id删除用户角色关联】的操作，参数：{}", id);
        return userRoleMapper.deleteByUserId(id);
    }

    @Override
    public int countByUserId(Long id) {
        log.debug("开始执行【根据user_id统计用户角色关联的数量】的数据访问，参数：{}", id);
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        return userRoleMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(UserRole userRole) {
        log.debug("开始执行【添加用户角色】的操作，参数：{}", userRole);
        return userRoleMapper.insert(userRole);
    }

    @Override
    public Long getRoleIdByUserId(Long id) {
        log.debug("开始执行【根据user_id获取role_id】的操作，参数：{}", id);
        return userRoleMapper.getRoleIdByUserId(id);
    }

    @Override
    public int update(UserRole userRole) {
        log.debug("开始执行【修改角色】的操作，参数：{}", userRole);
        return userRoleMapper.updateByUserId(userRole.getUserId(), userRole.getRoleId());
    }
}
